package com.csweimei.rentmanagementtreasure.pop;

import android.content.Context;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.csweimei.rentmanagementtreasure.R;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

public class PopChooseXq extends BottomPopupView {

    private Context mContext;
    private PopSelBack mClick;


    private List<PopBean> ysList = new ArrayList<>();

    private List<PopBean> areaList = new ArrayList<>();

    private List<PopBean> hxList = new ArrayList<>();
    private List<PopBean> cxList = new ArrayList<>();
    private List<PopBean> otherList = new ArrayList<>();

    RecyclerView hxRecy,cxRecy,otherRecy,ysRecy,areaRecy;
    private String ys,hx,area,cx,other;

    private TextView cancel,sure;
    AttachAdapter ysAdapter,hxAdapter,areaAdapter,cxAdapter,otherAdapter;


    public interface PopSelBack{
        void back(String ysT,String hxT,String areaT,String cxT,String otherT);
    }


    public PopChooseXq(@NonNull Context context, PopSelBack click) {
        super(context);
        mContext = context;
        this.mClick = click;
    }


    public void setChoose(String ysT,String hxT,String areaT,String cxT,String otherT){
        ys = ysT;
        hx = hxT;
        area = areaT;
        cx = cxT;
        other = otherT;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_choose_xq;
    }

    @Override
    protected void onCreate() {
        initData();

        ysRecy = findViewById(R.id.ys_recy);
        areaRecy = findViewById(R.id.area_recy);
        hxRecy = findViewById(R.id.hx_recy);
        cxRecy = findViewById(R.id.cx_recy);
        otherRecy = findViewById(R.id.other_recy);

        cancel = findViewById(R.id.cancel);
        sure = findViewById(R.id.sure);
        cancel.setText("重置");

        cancel.setOnClickListener(view ->{
            ysAdapter.setSel(-1);
            hxAdapter.setSel(-1);
            areaAdapter.setSel(-1);
            cxAdapter.setSel(-1);
            otherAdapter.setSel(-1);
            ys = "";
            hx = "";
            area = "";
            cx = "";
            other = "";
        });
        sure.setOnClickListener(view ->{
            if (mClick != null){
                mClick.back(ys,hx,area,cx,other);
            }
            dismiss();

        });

        initYs();
        initHx();
        initArea();
        initCx();
        initOther();



        for (int i = 0; i < ysList.size(); i++) {
            if (ysList.get(i).getName().equals(ys)){
                ysAdapter.setSel(i);
            }
        }

        for (int i = 0; i < hxList.size(); i++) {
            if (hxList.get(i).getName().equals(hx)){
                hxAdapter.setSel(i);
            }
        }


        for (int i = 0; i < areaList.size(); i++) {
            if (areaList.get(i).getName().equals(area)){
                areaAdapter.setSel(i);
            }
        }


        for (int i = 0; i < cxList.size(); i++) {
            if (cxList.get(i).getName().equals(cx)){
                cxAdapter.setSel(i);
            }
        }

        for (int i = 0; i < otherList.size(); i++) {
            if (otherList.get(i).getName().equals(other)){
                otherAdapter.setSel(i);
            }
        }

    }

    private void initYs() {
        ysAdapter = new AttachAdapter();
        ysRecy.setAdapter(ysAdapter);
        ysAdapter.addChildClickViewIds(R.id.tv);
        ysAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            ysAdapter.setSel(position);
            ys = ysList.get(position).getName();
        });
        ysAdapter.setList(ysList);
    }

    private void initHx() {
        hxAdapter = new AttachAdapter();
        hxRecy.setAdapter(hxAdapter);
        hxAdapter.addChildClickViewIds(R.id.tv);
        hxAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            hxAdapter.setSel(position);
            hx = hxList.get(position).getName();
        });
        hxAdapter.setList(hxList);
    }

    private void initArea() {
        areaAdapter = new AttachAdapter();
        areaRecy.setAdapter(areaAdapter);
        areaAdapter.addChildClickViewIds(R.id.tv);
        areaAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            areaAdapter.setSel(position);
            area = areaList.get(position).getName();
        });
        areaAdapter.setList(areaList);
    }

    private void initCx() {
        cxAdapter = new AttachAdapter();
        cxRecy.setAdapter(cxAdapter);
        cxAdapter.addChildClickViewIds(R.id.tv);
        cxAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            cxAdapter.setSel(position);
            cx = cxList.get(position).getName();
        });
        cxAdapter.setList(cxList);
    }

    private void initOther() {
        otherAdapter = new AttachAdapter();
        otherRecy.setAdapter(otherAdapter);
        otherAdapter.addChildClickViewIds(R.id.tv);
        otherAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            otherAdapter.setSel(position);
            other = otherList.get(position).getName();

        });
        otherAdapter.setList(otherList);
    }

    private void initData() {
        PopBean popBean;
        String[] ysStr = mContext.getResources().getStringArray(R.array.rentList);
        for (int i = 0; i < ysStr.length; i++) {
            popBean = new PopBean(ysStr[i]);
            ysList.add(popBean);
        }

        String[] hxStr = mContext.getResources().getStringArray(R.array.HouseTypeList);
        for (int i = 0; i < hxStr.length; i++) {
            popBean = new PopBean(hxStr[i]);
            hxList.add(popBean);
        }

        String[] areaStr = mContext.getResources().getStringArray(R.array.areaList);
        for (int i = 0; i < areaStr.length; i++) {
            popBean = new PopBean(areaStr[i]);
            areaList.add(popBean);
        }

        String[] cxStr = mContext.getResources().getStringArray(R.array.cxList);
        for (int i = 0; i < cxStr.length; i++) {
            popBean = new PopBean(cxStr[i]);
            cxList.add(popBean);
        }


        otherList.add(new PopBean("有电梯",1));
        otherList.add(new PopBean("无电梯",1));
        otherList.add(new PopBean("有照片",1));
        otherList.add(new PopBean("无照片",1));

    }

    private class AttachAdapter extends BaseQuickAdapter<PopBean, BaseViewHolder>{

        private int index = -1;

        public AttachAdapter() {
            super(R.layout.item_month_rent);
        }

        public void setSel(int flag){
            index = flag;
            notifyDataSetChanged();
        }

        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, PopBean s) {
            baseViewHolder.setText(R.id.tv,s.getName());

            if (getItemPosition(s)== index){
                baseViewHolder.setBackgroundResource(R.id.tv,R.drawable.rect_main_8);
                baseViewHolder.setTextColorRes(R.id.tv,R.color.white);
            }else {
                baseViewHolder.setBackgroundResource(R.id.tv,R.drawable.rect_f6_8);
                baseViewHolder.setTextColorRes(R.id.tv,R.color.text_9d);
            }
        }
    }


}
